package com.shwread.android.qysw.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shwread.android.qysw10000182.R;
import com.shwread.android.ui.widget.BookMarkDialog;
import com.shwread.android.ui.widget.BookMarkDialog.OnBookMarkMenuClickListener;
import com.tgx.tina.android.task.ATaskService;

import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
import org.geometerplus.fbreader.book.Bookmark;
import org.geometerplus.fbreader.book.Bookmark.DateType;
import org.geometerplus.fbreader.fbreader.FBReaderApp;

import java.util.ArrayList;
import java.util.List;
import logic.action.GetOneBooknotesAction;
import logic.action.GetOneBooknotesAction.OneBookNotesListener;
import logic.constants.DefaultConsts;
import logic.dao.BookMarksDao;
import logic.util.Util;


/**
 * @Project： Lqyd_Android
 * @Title：BookNotesDetailsPage
 * @Description：笔记详情
 * @Author：zhangnannan
 * @Date：2014年8月21日 上午11:02:19
 */
public class BookNotesDetailActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener,
        OneBookNotesListener,
        OnBookMarkMenuClickListener
{
	
	private String                              bookId    = "0";
	private String                              bookName  = "";
	private String                              Db_Id="1";
	private Context                             context;
	private Button                              btnBack;
	private TextView                            tvTitle;
	private ListView                            lvNotesDetail;
	private NotesDetailAdapter                  notesDetailAdapter;
	private List<Bookmark>                      bookMarksList;
	
	public FBReaderApp       myFBReaderApp;
	
	GetOneBooknotesAction<BaseBusinessActivity> getOneBooknotesAction;
	
	private Handler                             myHandler = new Handler()
	                                                      {
		                                                      
		                                                      @Override
		                                                      public void handleMessage(Message msg) {
			                                                      switch (msg.what) {
																	  case DefaultConsts.UPDATEUI_GETONEBOOK_BOOKNOTES:
																		  notesDetailAdapter.notifyDataSetChanged();
																		  break;
																	  
																	  default:
																		  break;
																  }
															  }
															  
	                                                      };
	
	@Override
	public Handler getUIHandler() {
		return myHandler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.book_notes_details);
		
		context = this;
		mAService = new ATaskService();
		mAService.startAService(context);
		
		Bundle b = getIntent().getExtras();
		if (null != b)
		{
			bookId = b.getString(DefaultConsts.book_contentId_s);
			bookName = b.getString(DefaultConsts.book_name);
			Db_Id = b.getString(DefaultConsts.db_id_s);
		}
		
		myFBReaderApp = (FBReaderApp) FBReaderApp.Instance();
		if (myFBReaderApp == null)
		{
			myFBReaderApp = new FBReaderApp(new BookCollectionShadow());
		}
		
		initView();
		setListener();
		initData();
	}
	
	@Override
	protected void onResume() {
		startAction();
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		mAService.stopAService();
		super.onDestroy();
	}
	
	void startAction() {
		getOneBooknotesAction = new GetOneBooknotesAction<BaseBusinessActivity>(BookNotesDetailActivity.this);
		Bundle bundle = new Bundle();
		bundle.putString(DefaultConsts.book_contentId_s, bookId);
		bundle.putString(DefaultConsts.book_name, bookName);
		bundle.putString(DefaultConsts.db_id_s, Db_Id);
		getOneBooknotesAction.OnSearchInfoListener(this);
		getOneBooknotesAction.start(bundle);
	}
	
	private void initData() {
		bookMarksList = new ArrayList<Bookmark>();
		notesDetailAdapter = new NotesDetailAdapter();
		lvNotesDetail.setAdapter(notesDetailAdapter);
	}
	
	private void setListener() {
		btnBack.setOnClickListener(this);
	}
	
	private void initView() {
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setText("笔记详情");
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		
		// mInputMethodManager = (InputMethodManager) bActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
		lvNotesDetail = (ListView) findViewById(R.id.book_notes_details_list_lv);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				finish();
				break;
			default:
				break;
		}
		
	}
	
	private class NotesDetailAdapter
	        extends
	        BaseAdapter
	{
		ViewHolder holder;
		
		@Override
		public int getCount() {
			return bookMarksList.size();
		}
		
		@Override
		public Object getItem(int arg0) {
			return bookMarksList.get(arg0);
		}
		
		@Override
		public long getItemId(int arg0) {
			return arg0;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			holder = new ViewHolder();
			if (convertView == null)
			{
				convertView = getLayoutInflater().inflate(R.layout.book_notes_details_list_item, null);
				holder.ivType = (ImageView) convertView.findViewById(R.id.book_notes_details_list_item_type_iv);
				holder.etContent = (TextView) convertView.findViewById(R.id.book_notes_details_list_item_content_tv);
				holder.etEndorse = (TextView) convertView.findViewById(R.id.book_notes_details_list_item_endores_tv);
				holder.tvDate = (TextView) convertView.findViewById(R.id.book_notes_details_list_item_date_tv);
				holder.ivEdit = (ImageView) convertView.findViewById(R.id.book_notes_details_list_item_edit_iv);
				holder.ivDelete = (ImageView) convertView.findViewById(R.id.book_notes_details_list_item_delete_iv);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			
			final Bookmark bookmark = (Bookmark) getItem(position);
			holder.etContent.setText(getResources().getString(R.string.note_item_highlight, bookmark.getText()));
			holder.tvDate.setText(Util.getFormatDate(bookmark.getDate(DateType.Creation).getTime()));
			if (bookmark.getStyleId() == 3 && bookmark.getBookNote() != null)
			{
				holder.etEndorse.setVisibility(View.VISIBLE);
				holder.etEndorse.setText(getResources().getString(R.string.note_item_note, bookmark.getBookNote()));
			} else {
				holder.etEndorse.setVisibility(View.GONE);
			}
			
			holder.ivEdit.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(BookNotesDetailActivity.this, NotesActivity.class);
					NotesActivity.myBookmark = bookmark;
					intent.putExtra(NotesActivity.EXISTING_BOOKMARK_KEY, true);
					startActivity(intent);
				}
			});
			holder.ivDelete.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View arg0) {
					//						Util.showToast(bActivity, "您确定要删除笔记吗？");	
					BookMarkDialog bookMarkDialog = new BookMarkDialog(BookNotesDetailActivity.this, BookNotesDetailActivity.this);
					bookMarkDialog.setBookMark(bookmark);
					bookMarkDialog.setDeleteContent("确认删除笔记？");
					bookMarkDialog.show();
				}
			});
			
			return convertView;
		}
		
	}
	
	class ViewHolder
	{
		ImageView ivType, ivEdit, ivDelete;
		TextView  tvDate, etContent, etEndorse;
	}
	
	@Override
	public void setAllBookInfos(List<Bookmark> infos) {
		if(bookMarksList.size()>0)bookMarksList.removeAll(bookMarksList);
		for(Bookmark mark: infos){
			if(mark.getStyleId() ==1)continue;
			bookMarksList.add(mark);
		}
	}
	
	@Override
	public void deleteBookMark(Bookmark bookmark) {
		BookMarksDao.getInstance(context).deleteBookmark(bookmark);
		bookMarksList.remove(bookmark);
		notesDetailAdapter.notifyDataSetChanged();
	}
	
	@Override
	public void deleteBookNote(Bookmark bookmark) {
		BookMarksDao.getInstance(context).deleteBookmark(bookmark);
		myFBReaderApp.deleteRemarkIcon(bookmark);
		bookMarksList.remove(bookmark);
		notesDetailAdapter.notifyDataSetChanged();
	}
	
}
